﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ParadiseShophcm.Admin
{
    public partial class QuanLySP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadSanPham();
                LoadNgayThang();
                LoadLoaiSanPham();

            }
        }

        DataProvider dt = new DataProvider();
        public void LoadSanPham()
        {
            String sql = "select * from SanPham Where isDelete=0";
            if (dt.MoKetNoi())
            {
                grvDanhSachSP.DataSource = dt.ExecuteQuery(sql, null);
                grvDanhSachSP.DataBind();

            }
        }
        /// <summary>
        /// Load ngày tháng lên combobox
        /// </summary>
        public void LoadNgayThang()
        {
            for (int ngay = 1; ngay <= 31; ngay++)
            {
                drdNgay.Items.Add(ngay.ToString());
            }
            for (int thang = 1; thang <= 12; thang++)
            {
                drdThang.Items.Add(thang.ToString());
            }
        }
        /// <summary>
        /// load lem dropdownlist danh sach san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void LoadLoaiSanPham()
        {
            String sql = "Select * from TheLoai";
            drdLoaiSP.DataSource = dt.ExecuteQuery(sql, null);
            drdLoaiSP.DataTextField = "TenTheLoai";
            drdLoaiSP.DataValueField = "MaTheLoai";
            drdLoaiSP.DataBind();
        }
      
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LbEdit_Click(object sender, EventArgs e)
        {
            try
            {

                LinkButton lbEdit = (LinkButton)sender;

                Label LbMaSanPham = (Label)lbEdit.Parent.FindControl("LbMaSanPham");
                HidMaSanPham.Value = LbMaSanPham.Text + "";
                mdpeEdit.Show();
                LoadFromCapNhat(HidMaSanPham.Value + "");


            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('" + ex.ToString() + "');", true);
            }

        }
        public void LuuThongTinSanPham()
        {
            try
            {
                HttpPostedFile file = FulHinhAnh.PostedFile;

                string path = Server.MapPath("~/Upload/" + FulHinhAnh.FileName);
                FulHinhAnh.SaveAs(path);
                string duongdan = "Upload/" + FulHinhAnh.FileName.ToString();
                string date = drdNgay.SelectedItem.ToString() + "/" + drdThang.SelectedItem.ToString() + "/" + txtNam.Text;
                String sql = "INSERT INTO SanPham(TenSanPham,ChiTiet,Gia,MaTheLoai,HinhAnh,NgayDang)";
                sql += " VALUES('" + txtTenSP.Text + "','" + txtChiTietSP.Text + "','" + txtGia.Text + "','" + drdLoaiSP.SelectedValue.ToString() + "','" + duongdan + "','" + date + "')";
                int nam = int.Parse(txtNam.Text);
                if (nam > 2020 || nam <= 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Năm không hợp lệ');", true);

                }
                else
                {
                    dt.ExecuteQuery(sql, null);

                    ResetFromCapNhat();
                    LoadSanPham();
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('" + ex.ToString() + "');", true);
            }


        }
        public void ResetFromCapNhat()
        {
            txtMaSP.Text = "";
            txtTenSP.Text = "";
            txtChiTietSP.Text = "";
            txtGia.Text = "";
            drdLoaiSP.Text = "";
            drdNgay.Text = "";
            drdThang.Text = "";
            txtNam.Text = "";

        }
        /// <summary>
        /// load danh san pham theo ma san pham 
        /// </summary>
        /// <param name="MaSanPham"></param>

        private void LoadFromCapNhat(string MaSanPham)
        {
            DataTable tb = new DataTable();
            String sql = "SELECT SanPham.MaSanPham, SanPham.TenSanPham, SanPham.ChiTiet, SanPham.Gia, TheLoai.TenTheLoai";
            sql += " FROM  SanPham INNER JOIN";
            sql += " TheLoai ON SanPham.MaTheLoai = TheLoai.MaTheLoai";
            sql += " WHERE (SanPham.MaSanPham = '" + MaSanPham + "')";

            tb = dt.ExecuteQuery(sql, null);
            if (tb.Rows.Count > 0)
            {
                txtMaSP.Text = tb.Rows[0]["MaSanPham"] + "";
                txtMaSP.ReadOnly = true;
                txtTenSP.Text = tb.Rows[0]["TenSanPham"] + "";
                txtChiTietSP.Text = tb.Rows[0]["ChiTiet"] + "";
                drdLoaiSP.SelectedItem.Text = tb.Rows[0]["TenTheLoai"] + "";
            }

        }

        protected void LbDelete_Click(object sender, EventArgs e)
        {
            try
            {

                LinkButton LbDelete = (LinkButton)sender;

                Label LbMaSanPham = (Label)LbDelete.Parent.FindControl("LbMaSanPham");
                HidMaSanPham.Value = LbMaSanPham.Text + "";
                String sql = "Update SanPham set isDelete=1 Where MaSanPham='" + HidMaSanPham.Value + "'";
                dt.ExecuteQuery(sql, null);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Xóa Thành Công');", true);
                LoadSanPham();


            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('" + ex.ToString() + "');", true);
            }
        }

        protected void LbAdd_Click(object sender, EventArgs e)
        {
            mdpeEdit.Show();
            txtMaSP.ReadOnly = true;
        }

        protected void LbtSave_Click(object sender, EventArgs e)
        {
            mdpeEdit.Hide();
            LuuThongTinSanPham();


        }

        protected void lbtThoat_Click(object sender, EventArgs e)
        {

        }

        protected void btn_PU_Click(object sender, EventArgs e)
        {

        }

        protected void LbtUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                HttpPostedFile file = FulHinhAnh.PostedFile;
                string path = Server.MapPath("~/Upload/" + FulHinhAnh.FileName);
                FulHinhAnh.SaveAs(path);
                string duongdan = "Upload/" + FulHinhAnh.FileName.ToString();
                string date = drdNgay.SelectedItem.ToString() + "/" + drdThang.SelectedItem.ToString() + "/" + txtNam.Text;
                String sql = "Update SanPham set TenSanPham='" + txtTenSP.Text + "',ChiTiet='" + txtChiTietSP.Text + "',Gia='" + txtGia.Text + "',MaTheLoai='" + drdLoaiSP.SelectedValue.ToString() + "',HinhAnh='" + duongdan + "',NgayDang='" + date + "' Where MaSanPham='" + HidMaSanPham.Value + "'";
                dt.ExecuteQuery(sql, null);
                LoadSanPham();
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('" + ex.ToString() + "');", true);
            }
        }
    }
}